/*
 * Copyright (C) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef IMAGE_KNIFE_C_KEKE_TRANSFORMATION_OPTION_NAPI_H
#define IMAGE_KNIFE_C_KEKE_TRANSFORMATION_OPTION_NAPI_H

#include <string>
#include "napi/native_api.h"
#include "utils/log.h"
#include "utils/napi_parser.h"
#include "transformation/transformation_option.h"
namespace ImageKnifePro {

class TransformationOptionNapi : public TransformationOption {
public:
    std::string customTransformation;

    TransformationOptionNapi(napi_env env, napi_value jsObject)
    {
        napi_value value = NapiParser::ParseProperty(env, jsObject, "customTransformation");
        if (value != nullptr) {
            customTransformation = NapiParser::ParseString(env, value);
        }

        value = NapiParser::ParseProperty(env, jsObject, "f32");
        if (value != nullptr) {
            uint32_t length = 0;
            napi_status status = napi_get_array_length(env, value, &length);
            if (status == napi_ok) {
                napi_value element;
                for (int i = 0; i < length; i++) {
                    napi_get_element(env, value, i, &element);
                    f32.push_back(NapiParser::ParseFloat(env, element));
                }
            } else {
                IMAGE_KNIFE_LOG(LOG_ERROR, "Parse Transformation f32:napi_get_array_length failed");
            }
        }

        value = NapiParser::ParseProperty(env, jsObject, "imageSrc");
        if (value != nullptr) {
            NapiParser::ParseImageSource(imageSrc, env, value);
        }

        value = NapiParser::ParseProperty(env, jsObject, "signature");
        if (value != nullptr) {
            signature = NapiParser::ParseString(env, value);
        }
        ParseTransformationType(env, jsObject);
    }
private:
    void ParseTransformationType(napi_env env, napi_value jsObject)
    {
        napi_value value = NapiParser::ParseProperty(env, jsObject, "type");
        if (value != nullptr) {
            type = static_cast<TransformationType>(NapiParser::ParseInt(env, value));
        }
    }
};

}

#endif //IMAGE_KNIFE_C_KEKE_TRANSFORMATION_OPTION_NAPI_H
